<template>
  <div>
    <el-form :rules="rules" ref="loginForm" :model="loginForm" class="loginContainer">
      <h2 class="title">系统登录</h2>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" auto-complete="off" aria-placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" auto-complete="off" aria-placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
      <el-form-item >
        <el-button  style="width: 45%" @click="resetForm('loginForm')">重置</el-button>
        <el-button  style="width: 45%" @click="test()">测试</el-button>

        <el-button type="primary" style="width: 45%" @click="submitLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {postKeyValueRequest} from "../utils/api";
import {postRequest} from "../utils/api";

export default {
  name: "Login",
  data(){
    return {
      checked:'',
      loginForm:{
        username: 'xiangxiang222',
        password: '123',
        name: '孙尚香222'
      },
      rids:[
        1,2,3,4,5,6
      ],
      rules:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    test(){
      console.log(this.ids);
      let url ="/hr/add?username="+this.loginForm.username+"&password="+this.loginForm.password+"&rids";
      let rrids = this.rids;
      if(rrids.length>0){
        url += '=';
        for (let i = 0; i < rrids.length; i++) {
          url =url+ rrids[i]+'&rids=';
          if(i==rrids.length-2){
            url = url + rrids[i+1];
            break;
          }
        }
      }
      console.log(url);
      postRequest(url).then(res=>{
      })
    },
    resetForm(loginForm){
      this.$refs.loginForm.resetFileds();
    },
    submitLogin(){
      //alert("aaa")
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          //alert('submit!');
          postKeyValueRequest("/doLogin",this.loginForm).then(resp =>{
            console.log("通过了");
            if(resp){
              window.sessionStorage.setItem("user", JSON.stringify(resp.obj));
              this.$router.replace('/home');
            }
          })
        } else {
          this.$message.error("校验不通过！！！");
          return false;
        }
      });
    }
  }
}
</script>

<style >
  .loginContainer{
    border-radius: 15px;
    background-clip: padding-box;
    margin: 110px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: white;
    border: 1px solid rebeccapurple;
    box-shadow: 0 0 25px blanchedalmond;
  }
  .title{
    margin: 0px auto 30px auto;
    text-align: center;
  }
  .loginRemember{
    margin: 0px 0px 18px 0px;
    text-align: left;
  }

</style>